﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.IsolatedStorage;
using System.Drawing;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ArgusPCLib.Bird.WindowsDesktop
{
    public class Interface : IPlatformInterface
    {
		IsolatedStorageFile isolatedStorageFile;
		IsolatedStorageFileStream fileStream;

		public Stream GetUserDataStream()
		{
			if (this.isolatedStorageFile == null)
				this.isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();

			if (this.fileStream == null)
				this.fileStream = this.isolatedStorageFile.OpenFile("UserData.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
			
			return this.fileStream;
		}

		public void RegisterUnhandledExceptionEventHandler(UnhandledExceptionEventHandler unhandledExceptionEventHandler)
		{
			AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
				{
					UnhandledExceptionEventArgs eArgs = new UnhandledExceptionEventArgs(e.ExceptionObject, e.IsTerminating);
					unhandledExceptionEventHandler(sender, eArgs);
				};
		}

		public void RegisterExitEventHandler(ExitEventHandler exitEventHandler)
		{
			System.Windows.Forms.Application.ApplicationExit += (sender, e) => { exitEventHandler(); };
			System.Windows.Application.Current.Exit += (sender, e) => { exitEventHandler(); };
			AppDomain.CurrentDomain.DomainUnload += (sender, e) => { exitEventHandler(); };
		}

		void Application_ApplicationExit(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}

		public void AddScreenshots(Func<string, Stream> getStream)
		{
			// WinForms
			foreach (Form form in System.Windows.Forms.Application.OpenForms)
			{
				Action a = () =>
					{
						using (Bitmap bitmap = new Bitmap(form.Width, form.Height))
						{
							try{form.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));}
							catch{return;}
							using (Stream stream = getStream(form.Text+".png"))
							{
								bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
							}
						}
					};

				if (form.InvokeRequired)
					form.Invoke(a);
				else
					a();
			}

			//WPF
			foreach (Window window in System.Windows.Application.Current.Windows)
			{
				window.Dispatcher.Invoke(() =>
					{
						RenderTargetBitmap bitmap = new RenderTargetBitmap((int)window.ActualWidth, (int)window.ActualHeight, 96, 96, PixelFormats.Bgra32);
						try { bitmap.Render(window); }
						catch { return; }
						PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
						pngEncoder.Frames.Add(BitmapFrame.Create(bitmap));
						using (Stream stream = getStream(window.Title+".png"))
						{
							pngEncoder.Save(stream);
						}
					});
			}
		}

		public void Dispose()
		{
			this.fileStream.Dispose();
			this.isolatedStorageFile.Dispose();
		}
    }
}
